package com.atguigu.webapp.controller.login;



import com.atguigu.common.constant.LoginUserContext;
import com.atguigu.common.result.Result;
import com.atguigu.webapp.service.LoginService;
import com.atguigu.webapp.vo.user.LoginVo;
import com.atguigu.webapp.vo.user.UserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Tag(name = "登录管理")
@RequestMapping("/app/")
@CrossOrigin
public class LoginController {

    @Autowired
    private LoginService loginService;

    @GetMapping("login/getCode")
    @Parameters({
            @Parameter(name = "phone",description = "手机号",in = ParameterIn.DEFAULT)
    })
    @Operation(summary = "获取短信验证码")
    public Result<Object> getSMSCode(@RequestParam String phone) {
        Boolean b = loginService.getSMSCode(phone);
        return Result.ok(b);
    }

    @PostMapping("login")
    @Parameters({
            @Parameter(name = "loginVo",description = "登录vo对象",in = ParameterIn.DEFAULT)
    })
    @Operation(summary = "登录成功返回生成的token")
    public Result<String> login(@RequestBody LoginVo loginVo) {
        String token = loginService.login(loginVo);
        return Result.ok(token);
    }

    @GetMapping("info")
    @Operation(summary = "获取登录用户信息")
    public Result<UserInfoVo> getLoginUserInfo() {
        //从全局上下文类中线程变量中获取用户信息
        //调用登录服务获取用户信息
        UserInfoVo userInfoVo = loginService.getLoginUserInfoById(LoginUserContext.getLoginUser().getUserId());
        //返回用户信息
        return Result.ok(userInfoVo);
    }
}
